package com.remote.server.Asyn;

import com.alibaba.fastjson.JSONObject;
import com.remote.server.feigin.OrderServiceFeigin;
import com.remote.server.service.DispatchService;
import com.smartcar.common.domain.User;
import com.smartcar.common.domain.request.OrderRequest;
import com.smartcar.common.domain.response.OrderResponse;
import com.smartcar.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Log4j2
@Component
public class AsynTemplat {

    @Autowired
    private DispatchService dispatchService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private OrderServiceFeigin orderServiceFeigin;

    @PostConstruct
    public void task(){
        List<User> users = dispatchService.missedOrder();

        for (User user : users) {
            redisTemplate.opsForList().leftPush("driver", JSONObject.toJSONString(user));
        }



//        Result<ArrayList<OrderResponse>> arrayListResult = orderServiceFeigin.orderList();
//        redisTemplate.opsForList().leftPush("userOrder",JSONObject.toJSONString(arrayListResult));

//        List<OrderResponse> list = dispatchService.userOrder();
//        redisTemplate.opsForList().leftPush("userOrder",JSONObject.toJSONString(list));

//        Result<List<OrderRequest>> selectPcOrder = orderServiceFeigin.selectPcOrder();
//        redisTemplate.opsForList().leftPush("orderPc",JSONObject.toJSONString(selectPcOrder));
//        log.info("订单表成功同步到reids中");


        /**
         * 订单表存入redis
         */
        Result<List<OrderRequest>> result = orderServiceFeigin.selectPcOrder();
        List<OrderRequest> data = orderServiceFeigin.selectPcOrder().getData();
        for (OrderRequest datum : data) {

            //判断订单的状态，status = 4 就存入redis中
            if (datum.getOrderState()!=null && datum.getOrderState()==4){
                redisTemplate.opsForList().leftPush("order",JSONObject.toJSONString(datum));
            }
        }

        log.info("订单表数据同步完成");
    }
}
